# from django.shortcuts import render
# from django.http import HttpResponse
from django.shortcuts import render, HttpResponse
from app.models.delivery import Community, Point, Account
from django.views.decorators.http import require_http_methods


# Create your views here.


@require_http_methods(["POST", "GET"])
def qurey_list(request):
    """
    查询社区列表、账户列表
    :param request: HttpRequest, http请求
    :return: 社区列表、账户列表
    """
    communitylist = Community.objects.all()
    accountlist = Account.objects.all()
    context = {}
    context["communityList"] = communitylist
    context["accountList"] = accountlist
    return render(request, 'visites/delivery.html', context)


@require_http_methods(["POST", "GET"])
def add_community(request):
    """
    添加社区
    :param request: HttpRequest, http请求
    :return: Response, 对请求的响应
    """
    # 社区实体
    c = Community.objects()
    # 存放社区名字的列表
    clist = []
    for i in c:
        clist.append(i.name)
    name = request.POST.get("name")  # 对应页面
    if name is None or name is "":
        code = 0
    elif name in clist:
        code = 0
    else:
        code = 1
    if code is 1:
        # 添加到数据库
        comm = Community(name=name)
        comm.save()
    return HttpResponse(code)


@require_http_methods(["POST", "GET"])
def add_account(request):
    """
    新增配送系统账号
    :param request: HttpRequest, http请求
    :return: Response, 对请求的响应
    """
    code = 0
    # 账户用户名list
    alist = []
    name = request.POST.get("user_name")  # 对应页面
    password = request.POST.get("password")
    community_id = request.POST.get("community_id")
    # 用户名重复判断（用户名为唯一标识，不允许重复）
    accounts = Account.objects()
    for i in accounts:
        alist.append(i.user_name)
    # 判断并添加到数据库
    if name is None or name is "":
        code = 0
    elif name in alist:
        code = 2
    elif password is None or password is "":
        code = 0
    else:
        code = 1
    if code == 1:
        a = Account(user_name=name, password=password, community_id=community_id)
        a.save()
    return HttpResponse(code)


@require_http_methods(["POST", "GET"])
def add_point(request):
    """
    添加配送点
    :param request: HttpRequest, http请求
    :return:Response, 对请求的响应
    """
    plist = []
    code = 0
    point_name = request.POST.get("point_name")  # 对应页面
    community_id = request.POST.get("community_id")
    point_address = request.POST.get("point_address")
    # 拿到对应ID的社区实体
    community = Community.objects.get(id=community_id)
    # 对应配送点
    points = community.points
    # 检查配送点，存在不添加
    if point_name is None or point_name is "":
        code = 0
    elif point_address is None or point_address is "":
        code = 0
    elif len(points) == 0:
        code = 1
    else:
        for p in points:
            plist.append(p.point_name)
            if point_name in plist:
                code = 0
            else:
                code = 1
    if code == 1:
        # 添加到数据库
        points = Point(point_name=point_name, point_address=point_address, status="已显示")
        community.points.append(points)
        community.save()
    return HttpResponse(code)


@require_http_methods(["POST", "GET"])
def update_status(request):
    """
    更新配送点状态
    :param request: HttpRequest, http请求
    :return: Response, 对请求的响应
    """
    status = request.POST.get("status")
    community_id = request.POST.get("community_id")
    point_name = request.POST.get("point_name")
    # 对应的社区实体
    community = Community.objects.get(id=community_id)
    # 对应配送点
    points = community.points
    for p in points:
        if p.point_name == point_name:
            p.status = status
            community.save()
    return HttpResponse(1)


@require_http_methods(["POST", "GET"])
def set_password(request):
    """
    设置密码
    :param request: HttpRequest, http请求
    :return:Response, 对请求的响应
    """
    code = 0
    account_id = request.POST.get("account_id")  # 对应页面
    password = request.POST.get("password")
    # 拿到对应ID的账户实体
    account = Account.objects.get(id=account_id)
    if password is None or password is "":
        code = 0
    else:
        code = 1
    if code == 1:
        # 更新数据
        account.update(password=password)
        account.save()
    return HttpResponse(code)
